﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Control;
using Enterprise.Model.Vote;
using Enterprise.Service.Vote;
namespace Enterprise.UI.Web.Modules.Vote
{
    public partial class Result : System.Web.UI.Page
    {
        string vId = (string)Utility.sink("vId", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            VoteService vService = new VoteService();
            var q = vService.VoteDisp(vId);
            if (q != null)
            {
                lb_Title.Text = q.VTITLE;
                lb_EndTime.Text = q.VENDTIME.ToShortDateString();
                lb_Remark.Text = q.VREMARK;
                lb_CUser.Text = Enterprise.Service.User.UserService.GetUserName(q.VCREATEUSER);
                lb_CTime.Text = q.VCREATETIME.ToString();

                VoteItemService viService = new VoteItemService();
                var list = viService.VoteItemList(vId).OrderBy(p => p.VITEMCODE);
                GridView1.DataSource = list;
                GridView1.DataBind();

                OnCommand();
            }
        }

        private void OnCommand()
        {
            CreateBT.AddButton("back.gif", "返回", "Vote.aspx?vId=" + vId, Utility.PopedomType.List, HeadMenu1);
        }

        protected string GetResult()
        {
            StringBuilder sb = new StringBuilder();
            VoteResultService vrService = new VoteResultService();
            var list = vrService.VoteResultList().Where(p => p.VID == vId);
            VoteItemService viService = new VoteItemService();
            var itemlist = viService.VoteItemList(vId);
            sb.Append("<table id='Result'>");
            sb.Append("<tr>");
            foreach (var var in itemlist)
            {
                sb.Append("<th>" + var.VITEMCODE + "</th>");
            }
            sb.Append("</tr>");
            sb.Append("<tr>");
            foreach (var var in itemlist)
            {                
                sb.Append("<td>" + list.Where(p => p.VITEMCODE == var.VITEMCODE).Count() + "</td>");                
            }
            sb.Append("</tr>");
            sb.Append("</table>");
            return sb.ToString();
        }

    }
}